const app = getApp()
import {
  getAllGoodsList
} from '../../api/user'

let currentPage = 1
let showCount = 10
let totalPage = 1
Page({
  data: {
    list: [],
    actionType: 4,
    sortType: 1,
    tabs: [
      {
        name: '综合',
        sortType: 1,
      },
      {
        name: '销量',
        sortType: 2,
      },
      {
        name: '价格',
        sortType: 3,
      }
    ],
    name:''
  },
  onShow() {
    this.initList()
  },
  onReachBottom(){
    if(currentPage>=totalPage){
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return 
    }
    currentPage++
    this.fetchList()
  },

  //#region  方法
  initList(){
    this.setData(
     {
      list: []
     }
    )
    totalPage = 1
    currentPage = 1
    this.fetchList()
  },
  fetchList(){
    const { sortType ,name ,actionType} = this.data
    getAllGoodsList({
      name,
      currentPage,
      showCount,
      sortType: sortType===3?actionType:sortType,
    }).then(res => {
      console.log(res)

      const { list, totalPage:total } = res.data
      if(total){
        totalPage = total
      }
      list.forEach( (item) => {
        if(item.pic){
          item.picUrl = item.pic.split(',')[0]
        }
        else{
          item.picUrl = ''
        }
        
      })
      this.setData({
        list: this.data.list.concat(list)
      })
    })
  },
  onSearch(){
    const { name } = this.data
    console.log('search')
    console.log(name)
    this.initList()
  },
  onChange(e){
    const value = e.detail
    this.setData({
      name: value
    })
  },
  changeSortType(e){
    const { sort } = e.currentTarget.dataset
    if(Number(sort)===3){
      console.log(Number(this.data.actionType)===3?4:3)
      
      this.setData({
        sortType: 3,
        actionType: Number(this.data.actionType)===3?4:3
      })
      this.initList()
      return;
    }

    if( Number(sort)===this.data.sortType){
      return ; 
    }


    this.setData({
      sortType: Number(sort)
    })
    this.initList()
  },
  ////#endregion 

})